﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using IDAL;
using System.Data.Entity;
namespace DAL
{
    public class SqlusersDAL : IusersDAL
    {
        public GameGOEntities db = DbContextFactory.CreateDbContext();

        public IEnumerable<users> GetAllUser()
        {
            return db.users;
        }

        public users GetUsersById(int id)
        {
            users item = db.users.Where(o => o.ID == id).FirstOrDefault();
            return item;
        }
        public users LoginValid(users user)
        {
            var target = db.users.Where(o => o.userName == user.userName && o.usesPwd == o.usesPwd).FirstOrDefault();
            return target;
        }

        public void ModifyState(int id)
        {
            var ss = db.users.Where(o => o.ID == id).FirstOrDefault();
            if (ss != null)
            {
                if (ss.nowState == 1)
                {
                    ss.nowState = 2;
                }
                else
                {
                    ss.nowState = 1;
                }
                db.SaveChanges();
            }
        }
        public void UpdateUserName(int uid, string un)
        {
            var user = db.users.Where(a => a.ID == uid).FirstOrDefault();
            user.userName = un;
            db.Set<users>().Attach(user);
            db.Entry<users>(user).State = EntityState.Modified;
            db.SaveChanges();
        }
        public void UpdateNickName(int uid, string un)
        {
            var user = db.users.Where(a => a.ID == uid).FirstOrDefault();
            user.nickName = un;
            db.Set<users>().Attach(user);
            db.Entry<users>(user).State = EntityState.Modified;
            db.SaveChanges();
        }
        public void UpdatePWD(int uid, string pwd)
        {
            var user = db.users.Where(a => a.ID == uid).FirstOrDefault();
            user.usesPwd = pwd;
            db.Set<users>().Attach(user);
            db.Entry<users>(user).State = EntityState.Modified;
            db.SaveChanges();
        }
        public void UpdateBirth(int uid, DateTime bt)
        {
            var user = db.users.Where(a => a.ID == uid).FirstOrDefault();
            user.Birthday = bt;
            db.Set<users>().Attach(user);
            db.Entry<users>(user).State = EntityState.Modified;
            db.SaveChanges();
        }
        public void UpdateDec(int uid, string bt)
        {
            var user = db.users.Where(a => a.ID == uid).FirstOrDefault();
            user.userDesc = bt;
            db.Set<users>().Attach(user);
            db.Entry<users>(user).State = EntityState.Modified;
            db.SaveChanges();
        }
    }
}
